package course.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Iterator;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.JTextComponent;

import course.controller.CourseControllerListener;
import course.po.Student;

public class StudentJPanel extends JPanel {
	private JTextField searchTextField;
	private static JScrollPane tablePanel;
	private static JPanel panel;
	public static int isbn = -1;
<<<<<<< HEAD
	public static int column = -1;
	public static String stuNo = null;
	private static CourseControllerListener controllerListener;
	public static JComboBox<String> comboBox;
	public static int stuId;
	public StudentJPanel() {
		this.controllerListener = new CourseControllerListener();
		this.controllerListener.setStudentPanel(this);

		setLayout(null);

		panel = new JPanel();
		panel.setBounds(15, 195, 1020, 387);
		panel.setBorder(new LineBorder(new Color(127, 157, 185), 1, false));

		panel.setLayout(new BorderLayout(0, 0));
		panel.add(this.getStudentTablePanel(controllerListener.getCourseService().findAllStudent()));
		add(panel);
		JPanel paneO = new JPanel();
		paneO.setBounds(15, 15, 1020, 150);
		paneO.setBorder(new LineBorder(new Color(127, 157, 185), 1, false));
		add(paneO);
		paneO.setLayout(null);

		JLabel searchLabel = new JLabel("搜索类型：");
		searchLabel.setFont(new Font("宋体", Font.PLAIN, 24));
		searchLabel.setBounds(97, 56, 126, 40);
		paneO.add(searchLabel);

		comboBox = new JComboBox<String>();// 创建一个下拉列表框comboBox
		comboBox.addItem("学生编号");
		comboBox.addItem("学生姓名");
		comboBox.setBounds(238, 62, 155, 33);
		paneO.add(comboBox);

		searchTextField = new JTextField();
		searchTextField.setBounds(461, 62, 245, 33);
		paneO.add(searchTextField);
		searchTextField.setColumns(10);

		JButton searchStudentButton = new JButton("查询");
		ImageIcon searchStudentIcon = new ImageIcon(StudentJPanel.class.getResource("/searchDean.png"));
		searchStudentButton.setIcon(searchStudentIcon);
		searchStudentButton.setBounds(779, 58, 106, 40);
		paneO.add(searchStudentButton);
		searchStudentButton.setFont(new Font("宋体", Font.PLAIN, 19));
		searchStudentButton.addActionListener(controllerListener);
		searchStudentButton.setActionCommand("searchStudentDialog");

		JPanel paneT = new JPanel();
		paneT.setBounds(15, 610, 1020, 86);
		add(paneT);
		paneT.setLayout(null);

		JButton updateStudentButton = new JButton("修改");
		ImageIcon updateStudentIcon = new ImageIcon(StudentJPanel.class.getResource("/updateDean.png"));
		updateStudentButton.setIcon(updateStudentIcon);
		updateStudentButton.setBounds(442, 15, 106, 40);
		paneT.add(updateStudentButton);
		updateStudentButton.setFont(new Font("宋体", Font.PLAIN, 19));
		updateStudentButton.addActionListener(controllerListener);
		updateStudentButton.setActionCommand("updateStudentDialog");

		JButton deleteStudentButton = new JButton("删除");
		ImageIcon deleteStudentIcon = new ImageIcon(StudentJPanel.class.getResource("/deleteDean.png"));
		deleteStudentButton.setIcon(deleteStudentIcon);
		deleteStudentButton.setBounds(716, 15, 106, 40);
		paneT.add(deleteStudentButton);
		deleteStudentButton.setFont(new Font("宋体", Font.PLAIN, 19));
		deleteStudentButton.addActionListener(controllerListener);
		deleteStudentButton.setActionCommand("deleteStudentDialog");

		JButton addStudentButton = new JButton("添加");
		ImageIcon addStudentIcon = new ImageIcon(StudentJPanel.class.getResource("/addDean.png"));
		addStudentButton.setIcon(addStudentIcon);
		addStudentButton.setBounds(156, 15, 106, 40);
		paneT.add(addStudentButton);
		addStudentButton.setFont(new Font("宋体", Font.PLAIN, 19));
		addStudentButton.addActionListener(controllerListener);
		addStudentButton.setActionCommand("addStudentDialog");

	}

	public static JScrollPane getStudentTablePanel(List<Student> studentList) {
		String titles[] = { "编号", "学号", "姓名", "性别", "年龄", "专业", "班级", "课程号", "课程名", "成绩" };
		Object[][] data = new Object[studentList.size()][10];
		Iterator<Student> iter = studentList.iterator();
		int i = 0;
		while (iter.hasNext()) {
			Student student = iter.next();
			data[i][0] = student.getStuId();
			data[i][1] = student.getStuNo();
			data[i][2] = student.getStuName();
			data[i][3] = student.getStuSex();
			data[i][4] = student.getStuAge();
			data[i][5] = student.getStuMajor();
			data[i][6] = student.getStuClassNo();
			data[i][7] = student.getCourse().getCouId();
			data[i][8] = student.getCourse().getCouName();
			data[i][9] = student.getStuGrade();
			i++;
		}
		DefaultTableModel tableModel = new DefaultTableModel(data, titles);
		JTable table = new JTable(tableModel);
		table.getTableHeader().setBackground(Color.orange); // 表头颜色
		table.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 17)); // 表头字体
		table.setFont(new Font("微软雅黑", Font.PLAIN, 25)); // 表体字体
		table.setBackground(Color.pink); // 表体颜色
		table.getTableHeader().setPreferredSize(new Dimension(1, 30)); // 表头高度
		table.setRowHeight(table.getRowHeight() + 15); // 表体高度
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // 表格只能选择一行
		table.repaint();
		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(final MouseEvent e) {
				isbn = table.getSelectedRow(); // 得到选中的行
				int row = table.getSelectedRow();
				column = table.getSelectedColumn();
//				TeacherFrame.t1.setText(table.getValueAt(row, column).toString());
//				TeacherFrame.t2.setText(table.getValueAt(row, column).toString());
				stuNo = table.getValueAt(row,1).toString();
//				stuId= Integer.valueOf(table.getValueAt(isbn, 0).toString());
			}
		});
		tablePanel = new JScrollPane(table);
		return tablePanel;
	}
//得到学生成绩的tableModel
	public static  JTable getStudentTable(List<Student> studentList) {
		String titles[] = {  "学号", "姓名", "课程名", "成绩" };
		Object[][] data = new Object[studentList.size()][4];
		Iterator<Student> iter = studentList.iterator();
		int i = 0;
		while (iter.hasNext()) {
			Student student = iter.next();
			data[i][0] = student.getStuNo();
			data[i][1] = student.getStuName();
			data[i][2] = student.getCourse().getCouName();
			data[i][3] = student.getStuGrade();
			i++;
		}
		DefaultTableModel tableModel = new DefaultTableModel(data, titles);
		 final JTable table = new JTable(tableModel );
	return table;
}
=======
	private static CourseControllerListener controllerListener;
	public static JComboBox<String> comboBox;

	public StudentJPanel() {
		this.controllerListener = new CourseControllerListener();
		this.controllerListener.setStudentPanel(this);

		setLayout(null);

		panel = new JPanel();
		panel.setBounds(15, 195, 1020, 387);
		panel.setBorder(new LineBorder(new Color(127, 157, 185), 1, false));

		panel.setLayout(new BorderLayout(0, 0));
		panel.add(this.getStudentTablePanel(controllerListener.getCourseService().findAllStudent()));
		add(panel);
		JPanel paneO = new JPanel();
		paneO.setBounds(15, 15, 1020, 150);
		paneO.setBorder(new LineBorder(new Color(127, 157, 185), 1, false));
		add(paneO);
		paneO.setLayout(null);

		JLabel searchLabel = new JLabel("搜索类型：");
		searchLabel.setFont(new Font("宋体", Font.PLAIN, 24));
		searchLabel.setBounds(97, 56, 126, 40);
		paneO.add(searchLabel);

		comboBox = new JComboBox<String>();// 创建一个下拉列表框comboBox
		comboBox.addItem("学生编号");
		comboBox.addItem("学生姓名");
		comboBox.setBounds(238, 62, 155, 33);
		paneO.add(comboBox);

		searchTextField = new JTextField();
		searchTextField.setBounds(461, 62, 245, 33);
		paneO.add(searchTextField);
		searchTextField.setColumns(10);

		JButton searchStudentButton = new JButton("查询");
		ImageIcon searchStudentIcon = new ImageIcon(StudentJPanel.class.getResource("/searchDean.png"));
		searchStudentButton.setIcon(searchStudentIcon);
		searchStudentButton.setBounds(779, 58, 106, 40);
		paneO.add(searchStudentButton);
		searchStudentButton.setFont(new Font("宋体", Font.PLAIN, 19));
		searchStudentButton.addActionListener(controllerListener);
		searchStudentButton.setActionCommand("searchStudentDialog");

		JPanel paneT = new JPanel();
		paneT.setBounds(15, 610, 1020, 86);
		add(paneT);
		paneT.setLayout(null);

		JButton updateStudentButton = new JButton("修改");
		ImageIcon updateStudentIcon = new ImageIcon(StudentJPanel.class.getResource("/updateDean.png"));
		updateStudentButton.setIcon(updateStudentIcon);
		updateStudentButton.setBounds(442, 15, 106, 40);
		paneT.add(updateStudentButton);
		updateStudentButton.setFont(new Font("宋体", Font.PLAIN, 19));
		updateStudentButton.addActionListener(controllerListener);
		updateStudentButton.setActionCommand("updateStudentDialog");

		JButton deleteStudentButton = new JButton("删除");
		ImageIcon deleteStudentIcon = new ImageIcon(StudentJPanel.class.getResource("/deleteDean.png"));
		deleteStudentButton.setIcon(deleteStudentIcon);
		deleteStudentButton.setBounds(716, 15, 106, 40);
		paneT.add(deleteStudentButton);
		deleteStudentButton.setFont(new Font("宋体", Font.PLAIN, 19));
		deleteStudentButton.addActionListener(controllerListener);
		deleteStudentButton.setActionCommand("deleteStudentDialog");

		JButton addStudentButton = new JButton("添加");
		ImageIcon addStudentIcon = new ImageIcon(StudentJPanel.class.getResource("/addDean.png"));
		addStudentButton.setIcon(addStudentIcon);
		addStudentButton.setBounds(156, 15, 106, 40);
		paneT.add(addStudentButton);
		addStudentButton.setFont(new Font("宋体", Font.PLAIN, 19));
		addStudentButton.addActionListener(controllerListener);
		addStudentButton.setActionCommand("addStudentDialog");

	}

	public static JScrollPane getStudentTablePanel(List<Student> studentList) {
		String titles[] = { "编号", "学号", "姓名", "性别", "年龄", "专业", "班级", "课程号", "课程名", "成绩" };
		Object[][] data = new Object[studentList.size()][10];
		Iterator<Student> iter = studentList.iterator();
		int i = 0;
		while (iter.hasNext()) {
			Student student = iter.next();
			data[i][0] = student.getStuId();
			data[i][1] = student.getStuNo();
			data[i][2] = student.getStuName();
			data[i][3] = student.getStuSex();
			data[i][4] = student.getStuAge();
			data[i][5] = student.getStuMajor();
			data[i][6] = student.getStuClassNo();
			data[i][7] = student.getCourse().getCouId();
			data[i][8] = student.getCourse().getCouName();
			data[i][9] = student.getStuGrade();
			i++;
		}
		DefaultTableModel tableModel = new DefaultTableModel(data, titles);
		JTable table = new JTable(tableModel);
		table.getTableHeader().setBackground(Color.orange); // 表头颜色
		table.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 17)); // 表头字体
		table.setFont(new Font("微软雅黑", Font.PLAIN, 25)); // 表体字体
		table.setBackground(Color.pink); // 表体颜色
		table.getTableHeader().setPreferredSize(new Dimension(1, 30)); // 表头高度
		table.setRowHeight(table.getRowHeight() + 15); // 表体高度
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // 表格只能选择一行
		table.repaint();
		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(final MouseEvent e) {
				isbn = table.getSelectedRow(); // 得到选中的行
				int row = table.getSelectedRow();
				int column = table.getSelectedColumn();
				TeacherFrame.t1.setText(table.getValueAt(row, column).toString());
				TeacherFrame.t2.setText(table.getValueAt(row, column).toString());
			}
		});
		tablePanel = new JScrollPane(table);
		return tablePanel;
	}

>>>>>>> refs/remotes/origin/master
	public static void refreshStudent(List<Student> studentList) {
		System.out.println("测试——当前学生数量为：" + studentList.size());
		panel.remove(tablePanel);
		panel.add(StudentJPanel.getStudentTablePanel(controllerListener.getCourseService().findAllStudent()),
				BorderLayout.CENTER);
		panel.validate();
		JOptionPane.showMessageDialog(panel, "重新加载数据成功！");

	}

	public void showStudentSearchResult(List<Student> result) {
		panel.remove(tablePanel);
		panel.add(this.getStudentTablePanel(result), BorderLayout.CENTER);
		this.validate();
		JOptionPane.showMessageDialog(this, "查询成功！");

	}

	public CourseControllerListener getControllerListener() {
		return controllerListener;
	}

	public int getStudentIsbn() {
		return isbn;
	}

	public JTextComponent getSearchTextField() {
		return searchTextField;
	}

}
